package design;

import java.util.ArrayList;
import java.util.List;

/**
 *      1
 *     1 1
 *    1 2 1
 *   1 3 3 1
 *  1 4 6 4 1
 * 1 5 10 10 5 1
 * 
 * @author Lei
 */
public class PrintTriangle {

    public static void printTriangle(int rows) {
        if (rows < 1) {
            return;
        }

        List<Integer> preLineList = new ArrayList<Integer>();
        List<Integer> curLineList = new ArrayList<Integer>();

        if (rows == 1) {
            System.out.println("1");
        }
        else if (rows == 2) {
            System.out.println("1");
            System.out.println("1 1");
        }
        else {
            System.out.println("1");
            System.out.println("1 1");

            preLineList.add(1);
            preLineList.add(1);
            for (int i = 3; i <= rows; i++) {
                curLineList.add(1);
                for (int j = 1; j < i - 1; j++) {
                    int newValue = preLineList.get(j - 1) + preLineList.get(j);
                    curLineList.add(newValue);
                }
                curLineList.add(1);

                printLine(curLineList);

                System.out.println();
                
                preLineList.clear();
                for (Integer oneElement : curLineList) {
                    preLineList.add(oneElement);
                }
                curLineList.clear();
                
            }
        }
    }

    private static void printLine(List<Integer> list) {
        for (Integer i : list) {
            System.out.print(i);
        }
    }

    public static void main(String args[]) {
        
        PrintTriangle.printTriangle(7);
        
    }
}
